
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:untitled1/app/broker/modules/my/BrokerageInfoPage.dart';
import 'package:untitled1/app/utils/utilRouter.dart';
import 'package:untitled1/app/widgets/toast/MyToast.dart';

import '../../../res/myColors.dart';
import '../../../utils/utilScreen.dart';
import '../../../widgets/TextSelector.dart';
import '../../../widgets/TitleBar.dart';
import '../../../widgets/base_page/BaseStatefulPage.dart';
import '../employer/EmployerPageListController.dart';
import 'BrokeraItemPageController.dart';
import 'BrokeragePageController.dart';
class BrokeraItemPage extends BaseStatefulPage {
  int page_type = 0;
  int  page_index=0;

  BrokeraItemPage(this.page_type,this.page_index);

  @override
  BaseStatefulPageState<BaseStatefulPage> createState() {
    return _BrokeraItemPage();
  }
}

class _BrokeraItemPage extends BaseStatefulPageState<BrokeraItemPage> {
  //刷新控制器
  final RefreshController _refreshController = RefreshController(
    initialRefresh: false,
  );
  final  controllerData = Get.put(BrokeraItemPageController());

  // 模拟下拉刷新
  void _onRefresh() async {
    // 模拟网络请求延时
    List<dynamic> list=await controllerData.fetchData();
    print(">]数据刷新  type1=${widget.page_type},list.isze=${list.length}");
    controllerData.list.clear();
    controllerData.list.addAll(list);
    controllerData.list.refresh();

    // print(">]请求数据成功 type2=${widget.type},list.isze=${ controllerData.list.length}");
    // 结束刷新
    _refreshController.refreshCompleted();
    setState(() {

    });
  }

  // 模拟上拉加载更多
  void _onLoading() async {
    List<dynamic> list=await controllerData.fetchData();
    print(">]加载更多 type1=${widget.page_type},list.isze=${list.length}");
    // 模拟网络请求延时
    if (list.isEmpty) {
      // 没有更多数据
      _refreshController.loadNoData();
    } else {
      controllerData.list.addAll(list);
      controllerData.list.refresh();
      _refreshController.loadComplete();
    }
    print(">]加载更多 type1=${widget.page_type},list.isze=${controllerData.list.length}");
    // 加载完成
    setState(() {

    });
  }

  @override
  void didUpdateWidget(covariant BrokeraItemPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('>]didUpdateWidget type =${widget.page_type},,,isActive=${widget.page_index}---${widget.page_index==widget.page_type?'更新':'不更新'}');
    // _onRefresh();
    if(widget.page_index==widget.page_type){
      WidgetsBinding.instance.addPostFrameCallback((_)  {
        _refreshController.requestRefresh();
      });
    }
  }
  @override
  void dispose() {
    _refreshController.dispose(); // 释放资源
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    if(widget.page_index==widget.page_type){
      WidgetsBinding.instance.addPostFrameCallback((_)  {
        _refreshController.requestRefresh();
      });
    }
  }

  @override
  Widget buildPage(BuildContext context) {
    return  SmartRefresher(
      controller: _refreshController,
      enablePullDown: true,  // 开启下拉刷新
      enablePullUp: true,  // 开启上拉加载
      onRefresh: _onRefresh,   // 下拉刷新回调
      onLoading: _onLoading,  // 上拉加载回调

      //头部样式
      header: ClassicHeader(
        refreshingText: "正在刷新...",
        idleText: "下拉可以刷新",
        completeText: "刷新完成",
        failedText: "刷新失败",
        releaseText: "松开立即刷新",
        textStyle: TextStyle(color: Colors.black87),
      ),
      // 自带经典尾部样式
      footer: ClassicFooter(
        noDataText: "没有更多数据",
        loadingText: "加载中...",
        idleText: "上拉加载更多",
        failedText: "加载失败，请重试",
      ),
      child: page(),
    );  //   Text("type=${widget.page_type}"),
  }
  Widget page(){
    return ListView.separated(
      itemCount: controllerData.list.length,
      itemBuilder: (context,index){
        return buildItem();
      },
      separatorBuilder: ( context,  index){
        return Divider(height: 10,color: myColors.c_D1D3D5,);
      },

    );
  }

  Widget buildItem(){
    return InkWell(
      onTap: () {

        MyToast.showSystemToast("aaaaa");
        utilRouter.to(BrokerageInfoPage("a"));
      },
      child: Row(
        children: [

          Expanded(
              flex: 6,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  buildItemContent( '${widget.page_type}编号：xxx'),
                  buildItemContent('分账时间：xxx'),
                  buildItemContent('分账类别：xxx'),
                  buildItemContent('状态：xxx'),
                ],
              )),
          Expanded(
              flex: 4,
              child: Container(
                padding: EdgeInsets.only(right: 10),
                alignment: Alignment.centerRight,
                child: Text(
                  '+88',
                  style: TextStyle(fontSize: 14, color: myColors.red),
                ),
              )
          ),
        ],
      ),
    );
  }
  Widget buildItemContent( String title) {
    return Container(
      margin: EdgeInsets.only(left: 15, right: 15, top: 5),
      child: Text(
        '${title}',
        style: TextStyle(fontSize: 14, color: myColors.c_707070),
      ),
    );
  }
  Widget buildItemContent2(String title, String content) {
    return InkWell(
        onTap: () {
          MyToast.showSystemToast("查看合同");
        },
        child: Container(
          margin: EdgeInsets.only(left: 15, right: 15, top: 5),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '${title}',
                style: TextStyle(fontSize: 14, color: myColors.c_707070),
              ),
              Text(
                '${content}',
                style: TextStyle(fontSize: 14, color: myColors.red),
              )
            ],
          ),
        )
    );
  }
}